<!DOCTYPE HTML>
<!--
	Dimension by HTML5 UP
	html5up.net | @ajlkn
	Free for personal and commercial use under the CCA 3.0 license (html5up.net/license)
-->
<html>
 <head>
  <title>
   Dimension by HTML5 UP
  </title>
  <!-- <meta charset="utf-8" /> -->
  <!-- <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" /> -->
  <meta charset="utf-8"/>
  <meta content="width=device-width,initial-scale=1.0" name="viewport"/>
  <link href="../../assets/css/article.css" rel="stylesheet"/>
  <link href="https://cdn.bootcss.com/highlight.js/9.15.8/styles/github.min.css" rel="stylesheet"/>
  <noscript>
   <link href="../../assets/css/noscript.css" rel="stylesheet"/>
  </noscript>
 </head>
 <body>
  <div id="app">
  </div>
  <!-- built files will be auto injected -->
 </body>
 <body class="is-preload">
  <!-- Wrapper -->
  <div id="wrapper">
   <!-- Main -->
   <div id="main">
    <article id="article">
     <h1 id="spring-boot-gradle">
      Spring boot Gradle项目搭建
     </h1>
     <hr/>
     <h3 id="ideagradle">
      使用IDEA创建Gradle工程
     </h3>
     <p>
      操作大致为：File-&gt;new-&gt;Project-&gt;Gradle(在左侧选项栏中)
     </p>
     <p>
      创建常规以后生成的工程目录如下：
     </p>
     <ul>
      <li>
       build
      </li>
      <li>
       gradle
       <ul>
        <li>
         wrapper
         <ul>
          <li>
           gradle-wrapper.jar
          </li>
          <li>
           gradle-wrapper.properties
          </li>
         </ul>
        </li>
       </ul>
      </li>
      <li>
       src
       <ul>
        <li>
         java
        </li>
        <li>
         resources
        </li>
       </ul>
      </li>
      <li>
       test
       <ul>
        <li>
         java
        </li>
        <li>
         resources
        </li>
       </ul>
      </li>
      <li>
       build.gradle
      </li>
      <li>
       gradlew
      </li>
      <li>
       gradlew.bat
      </li>
      <li>
       settings.gradle
      </li>
     </ul>
     <h3 id="spring-boot">
      配置Spring boot
     </h3>
     <p>
      下面需要对两个文件进行编辑：
     </p>
     <p>
      build.gradle文件修改后的内容如下,依赖一般是前面是groupId，中间是artifactId，第三个一般是版本。在repositories配置使用阿里云的仓库，避免下载过慢。
     </p>
     <div class="codehilite">
      <pre><span></span><code>plugins <span class="o">{</span>
    id <span class="s1">'java'</span>
    id <span class="s1">'com.gradle.build-scan'</span> version <span class="s1">'2.0.2'</span>
    id <span class="s1">'org.springframework.boot'</span> version <span class="s1">'2.0.5.RELEASE'</span>
    id <span class="s1">'io.spring.dependency-management'</span> version <span class="s1">'1.0.7.RELEASE'</span>
<span class="o">}</span>

group <span class="s1">'seckill'</span>
version <span class="s1">'1.0-SNAPSHOT'</span>

<span class="nv">sourceCompatibility</span> <span class="o">=</span> <span class="m">1</span>.8

repositories <span class="o">{</span>
    maven <span class="o">{</span> url <span class="s2">"http://maven.aliyun.com/nexus/content/groups/public/"</span> <span class="o">}</span>
    mavenCentral<span class="o">()</span>
<span class="o">}</span>

dependencies <span class="o">{</span>
    testCompile group: <span class="s1">'junit'</span>, name: <span class="s1">'junit'</span>, version: <span class="s1">'4.12'</span>

    implementation <span class="s1">'org.springframework.boot:spring-boot-dependencies:2.0.5.RELEASE'</span>
    implementation <span class="s1">'org.springframework.boot:spring-boot-starter-web'</span>

    testImplementation <span class="s1">'org.springframework.boot:spring-boot-starter-test'</span>

    components <span class="o">{</span>
        withModule<span class="o">(</span><span class="s1">'org.springframework:spring-beans'</span><span class="o">)</span> <span class="o">{</span>
            allVariants <span class="o">{</span>
                withDependencyConstraints <span class="o">{</span>
                    // Need to patch constraints because snakeyaml is an optional dependency
                    it.findAll <span class="o">{</span> it.name <span class="o">==</span> <span class="s1">'snakeyaml'</span> <span class="o">}</span>.each <span class="o">{</span> it.version <span class="o">{</span> strictly <span class="s1">'1.19'</span> <span class="o">}</span> <span class="o">}</span>
                <span class="o">}</span>
            <span class="o">}</span>
        <span class="o">}</span>
    <span class="o">}</span>
<span class="o">}</span>

buildScan <span class="o">{</span>

    // always accept the terms of service
    <span class="nv">termsOfServiceUrl</span> <span class="o">=</span> <span class="s1">'https://gradle.com/terms-of-service'</span>
    <span class="nv">termsOfServiceAgree</span> <span class="o">=</span> <span class="s1">'yes'</span>

    // always publish a build scan
    publishAlways<span class="o">()</span>
<span class="o">}</span>
</code></pre>
     </div>
     <p>
      setting.gradle文件修改后的内容如下：
     </p>
     <div class="codehilite">
      <pre><span></span><code>rootProject.name <span class="o">=</span> <span class="s1">'seckill'</span>
enableFeaturePreview<span class="o">(</span><span class="s1">'IMPROVED_POM_SUPPORT'</span><span class="o">)</span>
</code></pre>
     </div>
     <h3 id="_1">
      编写类
     </h3>
     <p>
      首先在src/java下生成源码目录com.seckill.spring(相当于com/seckill/spring)
     </p>
     <p>
      在src/java下创建程序入口类Application.java,其内容如下：
     </p>
     <div class="codehilite">
      <pre><span></span><code><span class="kn">package</span> <span class="nn">com.seckill.spring</span><span class="p">;</span>

<span class="kn">import</span> <span class="nn">org.springframework.boot.SpringApplication</span><span class="p">;</span>
<span class="kn">import</span> <span class="nn">org.springframework.boot.autoconfigure.SpringBootApplication</span><span class="p">;</span>

<span class="nd">@SpringBootApplication</span>
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">Application</span> <span class="p">{</span>
    <span class="kd">public</span> <span class="kd">static</span> <span class="kt">void</span> <span class="nf">main</span><span class="p">(</span><span class="n">String</span><span class="o">[]</span> <span class="n">args</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">SpringApplication</span><span class="p">.</span><span class="na">run</span><span class="p">(</span><span class="n">Application</span><span class="p">.</span><span class="na">class</span><span class="p">,</span> <span class="n">args</span><span class="p">);</span>
    <span class="p">}</span>
<span class="p">}</span>
</code></pre>
     </div>
     <p>
      在src/java/com.seckill.spring下创建目录controllers，并在controllers目录创建类：HelloWorld，在其中定义根路由并返回Hello World，其代码如下：
     </p>
     <div class="codehilite">
      <pre><span></span><code><span class="kn">package</span> <span class="nn">com.seckill.spring.controllers</span><span class="p">;</span>

<span class="kn">import</span> <span class="nn">org.springframework.web.bind.annotation.RequestMapping</span><span class="p">;</span>
<span class="kn">import</span> <span class="nn">org.springframework.web.bind.annotation.RequestMethod</span><span class="p">;</span>
<span class="kn">import</span> <span class="nn">org.springframework.web.bind.annotation.RestController</span><span class="p">;</span>

<span class="kn">import</span> <span class="nn">java.util.HashMap</span><span class="p">;</span>
<span class="kn">import</span> <span class="nn">java.util.Map</span><span class="p">;</span>

<span class="nd">@RestController</span>
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">HelloWorld</span> <span class="p">{</span>
    <span class="nd">@RequestMapping</span><span class="p">(</span><span class="n">value</span> <span class="o">=</span> <span class="s">"/"</span><span class="p">,</span> <span class="n">method</span> <span class="o">=</span> <span class="n">RequestMethod</span><span class="p">.</span><span class="na">GET</span><span class="p">)</span>
    <span class="kd">public</span> <span class="n">Map</span> <span class="nf">helloWorld</span><span class="p">()</span> <span class="p">{</span>
        <span class="n">Map</span><span class="o">&lt;</span><span class="n">String</span><span class="p">,</span> <span class="n">Object</span><span class="o">&gt;</span> <span class="n">ret</span> <span class="o">=</span> <span class="k">new</span> <span class="n">HashMap</span><span class="o">&lt;&gt;</span><span class="p">();</span>
        <span class="n">ret</span><span class="p">.</span><span class="na">put</span><span class="p">(</span><span class="s">"ret"</span><span class="p">,</span> <span class="s">"Hello World"</span><span class="p">);</span>
        <span class="k">return</span> <span class="n">ret</span><span class="p">;</span>
    <span class="p">}</span>
<span class="p">}</span>
</code></pre>
     </div>
     <h3 id="_2">
      单元测试
     </h3>
     <p>
      使用spring boot mockMVC测试非常方便
     </p>
     <div class="codehilite">
      <pre><span></span><code><span class="kn">package</span> <span class="nn">code.controllers</span><span class="p">;</span>

<span class="kn">import</span> <span class="nn">org.junit.Test</span><span class="p">;</span>
<span class="kn">import</span> <span class="nn">org.junit.runner.RunWith</span><span class="p">;</span>
<span class="kn">import</span> <span class="nn">org.springframework.beans.factory.annotation.Autowired</span><span class="p">;</span>
<span class="kn">import</span> <span class="nn">org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest</span><span class="p">;</span>
<span class="kn">import</span> <span class="nn">org.springframework.test.context.junit4.SpringRunner</span><span class="p">;</span>
<span class="kn">import</span> <span class="nn">org.springframework.test.web.servlet.MockMvc</span><span class="p">;</span>

<span class="kn">import static</span> <span class="nn">org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get</span><span class="p">;</span>
<span class="kn">import static</span> <span class="nn">org.springframework.test.web.servlet.result.MockMvcResultMatchers.content</span><span class="p">;</span>
<span class="kn">import static</span> <span class="nn">org.springframework.test.web.servlet.result.MockMvcResultMatchers.status</span><span class="p">;</span>

<span class="nd">@RunWith</span><span class="p">(</span><span class="n">SpringRunner</span><span class="p">.</span><span class="na">class</span><span class="p">)</span>
<span class="nd">@WebMvcTest</span>
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">HelloWorldTest</span> <span class="p">{</span>
    <span class="nd">@Autowired</span>
    <span class="kd">private</span> <span class="n">MockMvc</span> <span class="n">mockMvc</span><span class="p">;</span>

    <span class="nd">@Test</span>
    <span class="kd">public</span> <span class="kt">void</span> <span class="nf">helloWorld</span><span class="p">()</span> <span class="kd">throws</span> <span class="n">Exception</span> <span class="p">{</span>
        <span class="k">this</span><span class="p">.</span><span class="na">mockMvc</span><span class="p">.</span><span class="na">perform</span><span class="p">(</span><span class="n">get</span><span class="p">(</span><span class="s">"/"</span><span class="p">))</span>
                <span class="p">.</span><span class="na">andExpect</span><span class="p">(</span><span class="n">status</span><span class="p">().</span><span class="na">isOk</span><span class="p">())</span>
                <span class="p">.</span><span class="na">andExpect</span><span class="p">(</span><span class="n">content</span><span class="p">().</span><span class="na">json</span><span class="p">(</span><span class="s">"{'ret': 'Hello World'}"</span><span class="p">));</span>
    <span class="p">}</span>
<span class="p">}</span>
</code></pre>
     </div>
     <h3 id="_3">
      运行访问
     </h3>
     <ul>
      <li>
       运行Application类，可以在控制台看到起默认监听在8080端口
      </li>
      <li>
       浏览器访问:localhost:8080,可以看到返回字符串Hello World
      </li>
     </ul>
     <h2 id="_4">
      参考链接
     </h2>
     <ul>
      <li>
       <a href="https://guides.gradle.org/building-spring-boot-2-projects-with-gradle/">
        1.Building Spring Boot 2 Applications with Gradle
       </a>
      </li>
      <li>
       <a href="https://www.baeldung.com/spring-boot">
        2.Learn Spring Boot
       </a>
      </li>
      <li>
       <a href="https://spring.io/guides/gs/rest-service/">
        3.Building a RESTful Web Service
       </a>
      </li>
      <li>
       <a href="https://blog.csdn.net/ZZY1078689276/article/details/80364726">
        Gradle配置阿里云Maven镜像仓库地址
       </a>
      </li>
      <li>
       <a href="https://howtodoinjava.com/spring-boot2/spring-boot-mockmvc-example/">
        Spring boot MockMVC example
       </a>
      </li>
      <li>
       <a href="https://www.cnblogs.com/xiaoqi/p/6955288.html">
        Spring Boot配置文件放在jar外部
       </a>
      </li>
     </ul>
    </article>
   </div>
   <!-- Footer -->
   <footer id="footer">
    <p class="copyright">
     © Untitled. Design:
     <a href="https://html5up.net">
      HTML5 UP
     </a>
     .
    </p>
   </footer>
  </div>
  <!-- BG -->
  <div id="bg">
  </div>
  <!-- Scripts -->
  <script src="../assets/js/jquery.min.js">
  </script>
  <script src="../assets/js/browser.min.js">
  </script>
  <script src="../assets/js/breakpoints.min.js">
  </script>
  <script src="../assets/js/util.js">
  </script>
  <script src="../assets/js/main.js">
  </script>
 </body>
</html>
